package com.cloud.bc.util;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;

import javax.servlet.http.HttpServletRequest;

public class JiaoYouPageHelper {
    @Data
    private class PageModel {
        private int startPage;
        private int endPage;
        private int displayNum = 10;
    }

    public String getBcPageHTML(Page page, HttpServletRequest request) {
        int total = Convert.toInt(page.getTotal());
        if (total <= 0) {
            return "没有找到相关的内容";
        }
        int pageSize = Convert.toInt(page.getSize());
        int totalPage = (total + pageSize - 1) / pageSize;
        int current = Convert.toInt(page.getCurrent());
        StringBuffer displayInfo = new StringBuffer();
        displayInfo.append("<ul class=\"pagination\">");
        // 判断如果当前是第一页 则“首页”和“第一页”失去链接
        if (current <= 1) {
            displayInfo.append("<li class=\"disabled\"><span>«</span></li>");
        }
        if (current > 1) {
            displayInfo.append("<li><a href=\"" + convertListUrl(request, current - 1) + "\" rel=\"pre\">«</a></li>");
//            displayInfo.append("<a href=\"" + convertListUrl(request, current - 1) + "\">上一页</a>");
        }
        PageModel pageModel = new PageModel();
        countPages(current, totalPage, pageModel);
        for (int i = pageModel.getStartPage(); i <= pageModel.getEndPage(); i++) {
            if (i == current) {
                displayInfo.append("<li class=\"active\"><span>" + i + "</span></li>");
            } else {
                displayInfo.append("<li><a href=\"" + convertListUrl(request, i) + "\">" + i + "</a></li>");
            }
        }
        if (current < totalPage) {
            displayInfo.append("<li><a href=\"" + convertListUrl(request, current + 1) + "\" rel=\"next\">»</a></li>");
//            displayInfo.append("<a href=\"" + convertListUrl(request, current + 1) + "\">下一页</a>");
//            displayInfo.append("<a href=\"" + convertListUrl(request, totalPage) + "\">尾页</a>");
        }
        if (current == totalPage) {
            displayInfo.append("<li class=\"disabled\"><span>»</span></li> ");
        }

        displayInfo.append("</ul>");
        return displayInfo.toString();
    }

    public String convertListUrl(HttpServletRequest request, int current) {
        String uri = request.getRequestURI();
        String queryString = request.getQueryString();
        String pageData = "";
        if (StrUtil.isNotEmpty(queryString) && queryString.contains("page=")) {
            //存在则替换
            pageData = ReUtil.replaceAll(queryString, "page=(\\d+)", "page=" + current);
//            pageData = pageData.contains("?") ? pageData : "?" + pageData;
        } else {
            //不存在则添加
            pageData = StrUtil.isEmpty(queryString) ? "page=" + current : queryString + "&page=" + current;
        }
        return uri + "?" + pageData;
    }

    /**
     * 计算起始页和结束页
     */
    private void countPages(int current, int totalPage, PageModel pageModel) {
        if (current - pageModel.getDisplayNum() / 2 < 1) {
            pageModel.setStartPage(1);
            pageModel.setEndPage(pageModel.getDisplayNum() > totalPage ? totalPage : pageModel.getDisplayNum());
        } else if (current + pageModel.getDisplayNum() / 2 > totalPage) {
            int n = totalPage - pageModel.getDisplayNum() + 1;
            pageModel.setStartPage(n > 0 ? n : 1);
            pageModel.setEndPage(totalPage);
        } else {
            pageModel.setStartPage(current - pageModel.getDisplayNum() / 2);
            pageModel.setEndPage(pageModel.getStartPage() + pageModel.getDisplayNum() - 1);
        }
    }
}
